/*
 * Copyright 2020-2030 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/
package com.gitee.minimalismstyle.fresh.sys.service.impl

import com.gitee.minimalismstyle.fresh.common.core.domain.R
import com.gitee.minimalismstyle.fresh.common.core.domain.Simple
import com.gitee.minimalismstyle.fresh.common.data.beetl.service.impl.LongModelBaseServiceImpl
import com.gitee.minimalismstyle.fresh.common.security.domain.FreshGrantedAuthority
import com.gitee.minimalismstyle.fresh.common.security.util.SecurityContextUtils
import com.gitee.minimalismstyle.fresh.simple.api.service.SimpleSqlService
import com.gitee.minimalismstyle.fresh.sys.dao.SysDataPermissionDao
import com.gitee.minimalismstyle.fresh.sys.api.model.SysDataPermission
import com.gitee.minimalismstyle.fresh.sys.api.service.SysDataPermissionService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Primary
import org.springframework.stereotype.Service

/**
 * 数据权限业务实现
 * @author maochao
*/
@Primary
@Service
class SysDataPermissionServiceImpl: SysDataPermissionService, LongModelBaseServiceImpl<SysDataPermissionDao, SysDataPermission>() {

    @Autowired
    private lateinit var simpleSqlService : SimpleSqlService

    override fun findByCurrentUser(dataCode: String): R<List<Any>> {
        val values = arrayListOf<Any>()
        val user = SecurityContextUtils.user()
        val objectIds = arrayListOf<Long>()
        objectIds.add(user.tenantId)
        objectIds.add(user.id)
        objectIds.add(user.groupId)
        objectIds.addAll(user.authorities.map { (it as FreshGrantedAuthority).roleId })
        val sysDataPermissions = dao.findByDataCodeAndByObjectIds(dataCode, objectIds)
        val sysDataPermission = sysDataPermissions.find { it.objectType == "ALL" }
        if(sysDataPermission == null){
            values.add(-1)
            sysDataPermissions.forEach { sysDataPermission ->
                when (sysDataPermission.dataValueType) {
                    "CONST" -> {
                        values.add(sysDataPermission.dataValue!!)
                    }
                    "VAR" -> {
                        when (sysDataPermission.dataValue) {
                            "user.tenantId" -> {
                                values.add(user.tenantId)
                            }
                            "user.groupId" -> {
                                values.add(user.groupId)
                            }
                            "user.id" -> {
                                values.add(user.id)
                            }
                        }
                    }
                    "SQL" -> {
                        val r = simpleSqlService.executeSelectByCode(sysDataPermission.dataValue!!, Simple())
                        values.addAll(r.get().map { it["dataValue"]!! })
                    }
                }
            }
        }

        return R.success(values)
    }
}